import {DEVICE_CLOSE_EDIT, DEVICE_OPEN_EDIT, DEVICE_QUERY_CHANGE, QUERY} from '../types/DeviceType'

const initState = {
  deviceEditVisible: false,
  deviceInfoList: [],
  deviceDetail: {
    "id": null,
    "name": null,
    "common_code": null,
    "infrared_code": null,
    "offline_flag": null,
    "push_flag": null,
    "created_at": null,
    "updated_at": null
  },
  pageable: {
    pageCount: 10,
    page: 1,
  },
  page: {
    total: 0,
    size: 0,
    current: 0,
    pages: 0
  },
  deviceQueryParam: {
    onlineState: null,
    pushState: null,
    dateRange: [],
    deviceAddDateStart: null,
    deviceAddDateEnd: null
  }
}

const deviceReducer = (state = initState, dispatch) => {
  switch (dispatch.type) {
    case QUERY: {
      return {
        ...state,
        deviceInfoList: dispatch.payload.record,
        page:dispatch.payload.page
      }
    }
    case DEVICE_CLOSE_EDIT: {
      return {
        ...state,
        deviceEditVisible: false,
        deviceDetail: Object.assign({}, initState.deviceDetail)
      }
    }
    case DEVICE_OPEN_EDIT: {
      return {
        ...state,
        deviceEditVisible: true,
        deviceDetail: dispatch.payload
      }
    }
    case DEVICE_QUERY_CHANGE: {
      return {
        ...state,
        ...dispatch.payload
      }
    }
    default:
      return state
  }
}

export default deviceReducer